package com.example.twoscroolview.utils

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.twoscroolview.adapter.StickAdapter

class ItemDecoration(val context: Context) : RecyclerView.ItemDecoration() {

    private var mPaint: Paint = Paint();
    private var mTextPaint: Paint;

    private var textRect: Rect;

    init {
        mPaint.color = Color.RED
        mTextPaint = Paint()
        mTextPaint.color = Color.WHITE
        mTextPaint.textSize = 50f
        textRect = Rect()
    }

    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDraw(c, parent, state)
        if (parent.adapter is StickAdapter) {
            val adapter = parent.adapter as StickAdapter
            val childCount = parent.childCount
            val left = parent.paddingLeft
            val right = parent.width - parent.paddingRight
            for (count in 0 until childCount) {
                //当前屏幕item个数
                val view = parent.getChildAt(count)
                val childLayoutPosition = parent.getChildLayoutPosition(view)

                val headGroup = adapter.isHeadGroup(childLayoutPosition)
                val name = adapter.getGroupName(childLayoutPosition)+"onDraw"
                if (headGroup) {
                    c.drawRect(
                        left.toFloat(), (view.top - 100).toFloat(),
                        right.toFloat(), view.top.toFloat(), mPaint
                    )

                    mTextPaint.getTextBounds(name, 0, name.length, textRect)
                    c.drawText(
                        name, (left + 20).toFloat(), (view
                            .top - 100 / 2 + textRect.height() / 2).toFloat(), mTextPaint
                    )
                } else {
                    c.drawRect(
                        left.toFloat(), (view.top - 1).toFloat(),
                        right.toFloat(), view.top.toFloat(), mPaint
                    )
                }
            }


        }
    }

    override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDrawOver(c, parent, state)
        if (parent.adapter is StickAdapter) {
            val adapter = parent.adapter as StickAdapter
            //返回第一个可见的item
            val findFirstVisibleItemPosition =
                (parent.layoutManager as LinearLayoutManager).findFirstVisibleItemPosition()
            val itemView =
                parent.findViewHolderForLayoutPosition(findFirstVisibleItemPosition)?.itemView
            val paddingLeft = itemView?.paddingLeft
            val right = itemView?.width?.minus(itemView?.paddingRight!!)
            val top = parent.paddingTop
            //档第二个是头部的时候
            val headGroup = adapter.isHeadGroup(findFirstVisibleItemPosition+1)
            val name = adapter.getGroupName(findFirstVisibleItemPosition)+"onDrawOver"

            //是头部就预留更大的地方
            if (headGroup) {
                val bottom = Math.min(100,itemView?.bottom?:0)
                c.drawRect(
                    paddingLeft!!.toFloat(),
                    top.toFloat(),
                    right!!.toFloat(),
                    bottom.toFloat(),
                    mPaint
                )
                mTextPaint.getTextBounds(name, 0, name.length, textRect)
                c.drawText(
                    name, (paddingLeft + 20).toFloat(), (
                            top+bottom - 100 / 2 + textRect.height() / 2).toFloat(), mTextPaint
                )

            } else {
                c.drawRect(
                    paddingLeft!!.toFloat(),
                    top.toFloat(),
                    right!!.toFloat(),
                    top + 100.toFloat(),
                    mPaint
                )
                mTextPaint.getTextBounds(name, 0, name.length, textRect)
                c.drawText(
                    name, (paddingLeft + 20).toFloat(), (
                        top + 100 / 2 + textRect.height() / 2).toFloat(), mTextPaint
                )
            }
        }
    }

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        super.getItemOffsets(outRect, view, parent, state)

        if (parent.adapter is StickAdapter) {
            val adapter = parent.adapter as StickAdapter

            val position = parent.getChildLayoutPosition(view)


            val headGroup = adapter.isHeadGroup(position)
            //是头部就预留更大的地方
            if (headGroup) {
                outRect.set(0, 100, 0, 0)
            } else {
                outRect.set(0, 1, 0, 0)
            }
        }
    }
}